<?php
/**
 * Created by PhpStorm.
 * User: Silence
 * Date: 2017-07-12
 * Time: 14:18
 */

namespace App\Http\Controllers\Admin;

use App\Base\BaseFunc;
use App\Base\Power;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;

class AdminController extends Controller
{
    public function adminLogin()
    {
        return view("admin.adminLogin");
    }

    public function adminLogins(BaseFunc $baseFunc)
    {
        $input = Input::only('admin_name','admin_password');
        $name = 'admin';
        $password = 'admin';
        if ($input['admin_name'] == $name && $input['admin_password'] == $password)
        {
            session(['admin_id' => 1]);
            $baseFunc->setRedirectMessage(true,'登录成功',NULL,'/searchBusiness');
        }
        else
        {
            $baseFunc->setRedirectMessage(false,'登陆错误，请重新登陆！',NULL,'/adminLogin');
        }
    }

    public function adminLogout(BaseFunc $baseFunc)
    {
        session(["admin_id"=>NULL]);
        $baseFunc ->setRedirectMessage(true,"您已退出登录",NULL,"/adminLogin");
    }

    public function searchBusiness()
    {
        $data['businessData'] = DB::table('business')->get();
        return view('admin.searchBusiness',$data);
    }

    public function searchClass()
    {
        $data['classData'] = DB::table('goods_class')->get();
        return view('admin.classManage',$data);
    }

    public function addClass(BaseFunc $baseFunc)
    {
        $input = Input::only('goods_class_name');
        $check = DB::table('goods_class')
            ->where('goods_class_name','=',$input['goods_class_name'])
            ->get();
        if($check)
        {
            $baseFunc->setRedirectMessage(false,"您添加过此种类,请重新添加！",NULL,'/searchClass');
        }
        else
        {
            DB::table('goods_class')->insert($input);
            $baseFunc->setRedirectMessage(true,"您已成功添加种类",NULL,'/searchClass');
        }
    }

    public function delClass(BaseFunc $baseFunc)
    {
        $input = Input::only('goods_class_id');
        $data = DB::table('goods_class')
            ->where('goods_class_id','=',$input['goods_class_id'])
            ->delete();
        if($data)
        {
            $baseFunc->setRedirectMessage(true,'删除成功',NULL,'/searchClass');
        }
        else
        {
            $baseFunc->setRedirectMessage(false,'删除失败',NULL,'/searchClass');
        }
    }
}